package xin.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <pre>
 * itext练习-chunk
 * 块(Chunk)是能被添加到文档的文本的最小单位
 * 块 可以用于构建其他基础元素如短句、段落、锚点等，
 * 块 是一个有确定字体的字符串，要添加块到文档中时，其他所有布局变量均要被定义。
 * </pre>
 *
 * @author 马老七
 * @version 1.0
 * @since 2019/3/28
 */
public class ChunkTest {

    public static void main(String[] args) throws IOException, DocumentException {
        Rectangle rectangle = new Rectangle(PageSize.A4);
        rectangle.setBackgroundColor(BaseColor.WHITE);
        Document document = new Document(rectangle);
        FileOutputStream  fos = new FileOutputStream(new File("E:\\test\\pdf\\chunk.pdf"));
        PdfWriter writer = PdfWriter.getInstance(document,fos);
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2); //PDF版本(默认1.4)
        document.open();
        document.newPage();

        document.add(new Phrase("块（Chunk）测试",PdfFont.TITLE_FONT));
        PdfPTable table  = new PdfPTable(2);
        PdfPCell cell01 =  new PdfPCell(new Phrase("正常",PdfFont.NORMAL));
        cell01.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell01.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell01.setPadding(3f);
        cell01.setFixedHeight(30f);
        table.addCell(cell01);
        Chunk normal = new Chunk("iText Test", PdfFont.NORMAL);
        PdfPCell cell02 =  new PdfPCell(new Phrase(normal));
        cell02.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell02.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell02.setPadding(3f);
        table.addCell(cell02);

        PdfPCell cell11 =  new PdfPCell(new Phrase("下划线",PdfFont.TH_FONT));
        cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell11.setPadding(3f);
        cell11.setFixedHeight(30f);
        table.addCell(cell11);
        Chunk underline = new Chunk("iText Test", PdfFont.UNDERLINE);
        PdfPCell cell12 =  new PdfPCell(new Phrase(underline));
        cell12.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell12.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell12.setPadding(3f);
        table.addCell(cell12);

        PdfPCell cell21 =  new PdfPCell(new Phrase("中划线",PdfFont.TH_FONT));
        cell21.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell21.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell21.setPadding(3f);
        cell21.setFixedHeight(30f);
        table.addCell(cell21);
        Chunk strikethru = new Chunk("iText Test", PdfFont.STRIKETHRU);
        PdfPCell cell22 =  new PdfPCell(new Phrase(strikethru));
        cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell22.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell22.setPadding(3f);
        table.addCell(cell22);


        PdfPCell cell31 =  new PdfPCell(new Phrase("加粗",PdfFont.TH_FONT));
        cell31.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell31.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell31.setPadding(3f);
        cell31.setFixedHeight(30f);
        table.addCell(cell31);
        Chunk bold = new Chunk("iText Test", PdfFont.BOLD);
        PdfPCell cell32 =  new PdfPCell(new Phrase(bold));
        cell32.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell32.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell32.setPadding(3f);
        table.addCell(cell32);

        PdfPCell cell41 =  new PdfPCell(new Phrase("上标",PdfFont.TH_FONT));
        cell41.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell41.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell41.setPadding(3f);
        cell41.setFixedHeight(30f);
        table.addCell(cell41);
        Phrase superRisePhrase = new Phrase();
        Chunk superRiseText = new Chunk("iText Test", PdfFont.NORMAL);
        superRisePhrase.add(superRiseText);
        Chunk superscript = new Chunk(" 上标", PdfFont.RISE);
        superscript.setTextRise(8f);
        superRisePhrase.add(superscript);
        PdfPCell cell42 =  new PdfPCell(superRisePhrase);
        cell42.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell42.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell42.setPadding(3f);
        table.addCell(cell42);


        PdfPCell cell51 =  new PdfPCell(new Phrase("下标",PdfFont.TH_FONT));
        cell51.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell51.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell51.setPadding(3f);
        cell51.setFixedHeight(30f);
        table.addCell(cell51);
        Phrase subRisePhrase = new Phrase();
        Chunk subRiseText = new Chunk("iText Test", PdfFont.NORMAL);
        subRisePhrase.add(subRiseText);
        Chunk subscript = new Chunk(" 下标", PdfFont.RISE);
        subscript.setTextRise(-8f);
        subRisePhrase.add(subscript);
        PdfPCell cell52 =  new PdfPCell(subRisePhrase);
        cell52.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell52.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell52.setPadding(3f);
        table.addCell(cell52);

        PdfPCell cell61 =  new PdfPCell(new Phrase("斜体",PdfFont.TH_FONT));
        cell61.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell61.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell61.setPadding(3f);
        cell61.setFixedHeight(30f);
        table.addCell(cell61);
        Chunk italic = new Chunk("iText Test", PdfFont.ITALIC);
        PdfPCell cell62 =  new PdfPCell(new Phrase(italic));
        cell62.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell62.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell62.setPadding(3f);
        table.addCell(cell62);

        PdfPCell cell71 =  new PdfPCell(new Phrase("加粗斜体",PdfFont.TH_FONT));
        cell71.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell71.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell71.setPadding(3f);
        cell71.setFixedHeight(30f);
        table.addCell(cell71);
        Chunk boldItalic = new Chunk("iText Test", PdfFont.BOLDITALIC);
        PdfPCell cell72 =  new PdfPCell(new Phrase(boldItalic));
        cell72.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell72.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell72.setPadding(3f);
        table.addCell(cell72);


        PdfPCell cell81 =  new PdfPCell(new Phrase("未定义",PdfFont.TH_FONT));
        cell81.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell81.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell81.setPadding(3f);
        cell81.setFixedHeight(30f);
        table.addCell(cell81);
        Chunk undefined = new Chunk("iText Test", PdfFont.UNDEFINED);
        PdfPCell cell82 =  new PdfPCell(new Phrase(undefined));
        cell82.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell82.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell82.setPadding(3f);
        table.addCell(cell82);

        document.add(table);
        document.close();
        fos.close();
    }
}
